<?php
namespace app\service;
use think\Db;
class imageService
{
	public function createimg($visitor_id)
	{
		vendor('phpqrcode.phpqrcode');
		$data=array("visitor_id"=>$visitor_id,"user_type" => 1);
	    $url=json_encode($data);

	    $size=300;    //图片大小

	    $errorCorrectionLevel = "H"; // 容错级别：L、M、Q、H

	    $matrixPointSize = "10"; // 点的大小：1到10

	      //实例化

	    $qr = new \QRcode();
	    ob_end_clean();
	    $filename=dir(getcwd())->path."/static/ewm/".$visitor_id.".png";

	    $qr::png($url, $filename, $errorCorrectionLevel, $matrixPointSize,2);
	   	//$logo = dir(getcwd())->path."\T1hIhfXkBoXXXXXXXX.jpg";
	   	
	   	if(file_exists($filename)){
	   		return 1;
	   	}else{
	   		return 0;
	   	}
	}
    public function createwxuserimg($id)
    {
        vendor('phpqrcode.phpqrcode');
        $data=array("wxuser_id"=>$id,"user_type"=>2);
        $url=json_encode($data);

        $size=300;    //图片大小

        $errorCorrectionLevel = "H"; // 容错级别：L、M、Q、H

        $matrixPointSize = "10"; // 点的大小：1到10

        //实例化

        $qr = new \QRcode();
        ob_end_clean();
        $filename=dir(getcwd())->path."/static/ewm/wxuser/".$id.".png";

        $qr::png($url, $filename, $errorCorrectionLevel, $matrixPointSize,2);

        //$logo = dir(getcwd())->path."\T1hIhfXkBoXXXXXXXX.jpg";

        if(file_exists($filename)){
            return 1;
        }else{
            return 0;
        }
    }

    public function createimglogo($user_id,$logo_url)
	{
		vendor('phpqrcode.phpqrcode');

	    $url=$user_id;

	    $size=300;    //图片大小

	    $errorCorrectionLevel = "H"; // 容错级别：L、M、Q、H

	    $matrixPointSize = "10"; // 点的大小：1到10

	      //实例化

	    $qr = new \QRcode();
	    ob_end_clean();
	    $filename=dir(getcwd())->path."\static\\".$user_id.".png";
	    $qr::png($url, $filename, $errorCorrectionLevel, $matrixPointSize,2);
	   	//$logo = dir(getcwd())->path."\T1hIhfXkBoXXXXXXXX.jpg";
	   	$logo=$logo_url;
	   	 //准备好的logo图片
       
        $QR = $filename;      //已经生成的原始二维码图
       
        $QR = imagecreatefromstring(file_get_contents($QR));    //目标图象连接资源。
        $logo = imagecreatefromstring(file_get_contents($logo));  //源图象连接资源。
        $QR_width = imagesx($QR);      //二维码图片宽度
        $QR_height = imagesy($QR);     //二维码图片高度
        $logo_width = imagesx($logo);    //logo图片宽度
        $logo_height = imagesy($logo);   //logo图片高度
        $logo_qr_width = $QR_width / 4;   //组合之后logo的宽度(占二维码的1/5)
        $scale = $logo_width/$logo_qr_width;  //logo的宽度缩放比(本身宽度/组合后的宽度)
        $logo_qr_height = $logo_height/$scale; //组合之后logo的高度
        $from_width = ($QR_width - $logo_qr_width) / 2;  //组合之后logo左上角所在坐标点
            //重新组合图片并调整大小
            /*
            * imagecopyresampled() 将一幅图像(源图象)中的一块正方形区域拷贝到另一个图像中
            */
        imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width,$logo_qr_height, $logo_width, $logo_height);
       
            //输出图片
        imagepng($QR,$filename);
            
        return request()->domain(). $filename;
	}
}